package com.zfsoft.deployui.controller.deploy;

import com.zfsoft.deployui.deploy.Login;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Objects;

@Controller
public class LoginController {
    @RequestMapping(value = "/deploy/login",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String login(@RequestBody Login login, Map<String,Object> map, HttpSession session){
        System.out.println(login.toString());
        if(!Objects.equals("admin",login.getUser())||!Objects.equals("pass",login.getPassword())){
            session.invalidate();
            map.put("msg","用户名密码错误");
            return "Login Failed";
        }else{
            session.setAttribute("userName",login.getUser());
            return "success";
        }
    }
    @RequestMapping(value = "/deploy/logout")
    public String logout(HttpSession session,HttpServletResponse response ){
//        response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
//        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
//        response.setDateHeader("Expires", 0); // Proxies.
        session.invalidate();
        System.out.println(session.getId());
        return "redirect:/";
    }
}